package com.wbsy.officechat.broadcast;

import java.util.ArrayList;

import com.wbsy.officechat.StartActivity;
import com.wbsy.officechat.net.NetThreadHelper;
import com.wbsy.officechat.service.ListeneService;
import com.wbsy.officechat.util.CheckNet;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ServiceBroadCast extends BroadcastReceiver {
	NetThreadHelper netThreadHelper;

	@Override
	public void onReceive(Context arg0, Intent arg1) {
		netThreadHelper = NetThreadHelper.newInstance();
		ActivityManager myManager = (ActivityManager) arg0
				.getSystemService(Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
				.getRunningServices(30);
		for (int i = 0; i < runningService.size(); i++) {
			if (runningService.get(i).service.getClassName().toString()
					.equals("com.wbsy.officechat.service.ListeneService")) {
				// arg0.stopService(new Intent(arg0, ListeneService.class));
				new Thread(new Runnable() {

					@Override
					public void run() {
						netThreadHelper.refreshUsers();
					}
				}).start();
			} else {

			}
			if("android.net.conn.CONNECTIVITY_CHANGE".equals(arg1.getAction())){
				if(new CheckNet(arg0).checkNetConn()){
					arg0.startService(new Intent(arg0, ListeneService.class));
				}
			}else{
				arg0.startService(new Intent(arg0, ListeneService.class));
			}
		}

	}
}
